package com.rjwh.dingdong.client.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.aishua.lib.utils.AsStrUtils;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.bean.localbean.FilterItem;
import com.rjwh.dingdong.client.bean.localbean.TagFilter;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * 帖子标签view
 *
 * @author 王小珂
 * @ClassName TagLayout
 * @Description TODO(这里用一句话描述这个类的作用)
 * @date 2016-4-28 下午5:47:41
 * @history 1.YYYY-MM-DD author: description:
 */
public class TopicTagView extends LinearLayout {
    private OnTopicTagViewListener listener;
    private TextView radioGroup_name;
    private FlowRadioGroup radioGroup;
    //显示的标签
    private String filterId;
    //选中的子节点id
    private String currentChidViewId;

    public void setOnTopicTagViewListener(OnTopicTagViewListener listener) {
        this.listener = listener;
    }

    public TopicTagView(Context context) {
        super(context);
        init();
    }

    public TopicTagView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        final View view = inflate(getContext(), R.layout.community_post_headview_tagview, null);
        addView(view);
        radioGroup_name = (TextView) view.findViewById(R.id.radioGroup_name);
        radioGroup = (FlowRadioGroup) view.findViewById(R.id.radioGroup);
    }

    public void setTagData(final TagFilter filter, boolean hasAll) {
        if (!AsStrUtils.isEmpty(filter.getName())) {
            radioGroup_name.setText(filter.getName());
        }

        List<FilterItem> items = filter.getItems();
        filterId = filter.getId();
        setNewRadioButton(radioGroup, items.size(), filter, hasAll);
        for (int i = 0; i < items.size(); i++) {
            if (items.get(i).getActive().equals("1")) {
                currentChidViewId = items.get(i).getId();
            }
        }
        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
                List<FilterItem> items = filter.getItems();
                for (int i = 0; i < items.size(); i++) {
                    if (checkedId == i) {
                        toggleCheck(radioButton, items, i);
                        currentChidViewId = items.get(i).getId();
                        if (listener != null) {
                            listener.setTagCheck(TopicTagView.this);
                        }
                    }
                }
            }
        });
    }

    private void setNewRadioButton(RadioGroup radioGroup, int size, TagFilter filter, boolean hasAll) {
        RadioButton child[] = new RadioButton[size];
        for (int item = 0; item < size; item++) {
            child[item] = (RadioButton) LayoutInflater.from(getContext()).inflate(R.layout.community_tag_radio_button, null);
            String name = filter.getItems().get(item).getName().trim();
            String active = filter.getItems().get(item).getActive();
            child[item].setText(name);
            radioGroup.addView(child[item], getRadioGroupMargin());
            child[item].setId(item);
            if (active.equals("1")) {
                if (hasAll) {
                    radioGroup.check(item);// 设置被默认选中的项
                } else {
                    radioGroup.removeViewAt(item);
                }
            }
            ;
        }
    }

    protected void toggleCheck(RadioButton radioButton, List<FilterItem> items, int i) {
        if (radioButton.isChecked()) {
            items.get(i).setActive("1");
        } else {
            items.get(i).setActive("0");
        }
    }

    private RadioGroup.LayoutParams getRadioGroupMargin() {
        float density = getResources().getDisplayMetrics().density;
        RadioGroup.LayoutParams params_rb = new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        int leftMargin = (int) (5 * density);
        int topMargin = (int) (3 * density);
        params_rb.setMargins(leftMargin, topMargin, leftMargin, topMargin);
        return params_rb;
    }

    public String getFilterId() {
        return filterId;
    }

    public String getCurrentChidViewId() {
        return currentChidViewId;
    }

    public JSONObject getFilert() {

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("subid", currentChidViewId);
            jsonObject.put("id", filterId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

    public interface OnTopicTagViewListener {
        /**
         * 设置Tag选中
         *
         * @param @param view
         * @return void
         * @throws
         * @Title: setTagCheck
         * @Description: TODO(这里用一句话描述这个方法的作用)
         * @history 1.YYYY-MM-DD author: description:
         */
        void setTagCheck(TopicTagView view);
    }

}
